﻿using System;
using System.Collections.Generic;
using mki.Backup.GUI.Properties;
using mki.Backup.Objects;
using mki.Backup.Objects.ValidationObjects;

namespace mki.Backup.GUI.Helper
{
	/// <summary>
	/// Represents the ValidationExtension class
	/// </summary>
	internal static class ValidationExtension
	{
		#region Methods (1)

		// Public Static Methods (1) 

		/// <summary>
		/// Gets the error message.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns>returns a object of type String</returns>
		public static List<ValidationErrorItem> GetErrorMessages(this ValidationException source)
		{
			var errorItems = new List<ValidationErrorItem>();

			if (source.Data.Contains("errors"))
			{
				var validationResults = source.Data["errors"] as List<ValidationError>;
				if (validationResults != null)
				{
					foreach (var validationResult in validationResults)
					{
						var elementName = String.Empty;
						var referenceItem = validationResult.ReferenceItem as CustomConfigurationElement;
						if (referenceItem != null) { elementName = referenceItem.Name; }

						foreach (var item in validationResult.Items)
						{
							item.Message = String.Format("{0}", Resources.ResourceManager.GetString(String.Format("Error{0}", item.Field)));
							if (String.IsNullOrWhiteSpace(item.Message)) { item.Message = String.Format("No errormessage for {0}/{1} found", item.Field, item.Element); }
							item.Element = elementName;
						}

						errorItems.AddRange(validationResult.Items);
					}
				}
			}

			return errorItems;
		}

		#endregion Methods
	}
}
